home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP02.ZIP / CHAP02 / PATRON / PAGEWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  20KB  |  757 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Original Starter Chapter 2
  4.  *
  5.  * Window procedure for the Pages window and support functions.  This
  6.  * window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.  The public CPages::Print lives here.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19.  
  20. #include "patron.h"
  21.  
  22.  
  23. extern HWND g_hDlgPrint;
  24. extern BOOL g_fCancelPrint;
  25.  
  26.  
  27. /*
  28.  * PagesWndProc
  29.  *
  30.  * Purpose:
  31.  *  Window procedure for the Pages window.
  32.  */
  33.  
  34. LRESULT __export FAR PASCAL PagesWndProc(HWND hWnd, UINT iMsg
  35.     , WPARAM wParam, LPARAM lParam)
  36.     {
  37.     LPCPages        ppg;
  38.     PAINTSTRUCT     ps;
  39.     HDC             hDC;
  40.     int             iPos, iTmp;
  41.     int             iMin, iMax;
  42.     UINT            idScroll;
  43.  
  44.  
  45.     ppg=(LPCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  46.  
  47.     switch (iMsg)
  48.         {
  49.         case WM_CREATE:
  50.             ppg=(LPCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  51.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  52.  
  53.             ppg->m_hWnd=hWnd;
  54.             ppg->New();
  55.             break;
  56.  
  57.  
  58.         case WM_PAINT:
  59.             hDC=BeginPaint(hWnd, &ps);
  60.  
  61.             //Draw only if we have a page to show.
  62.             if (0!=ppg->m_cPages)
  63.                 ppg->Draw(hDC, FALSE, FALSE);
  64.  
  65.             EndPaint(hWnd, &ps);
  66.             break;
  67.  
  68.  
  69.         case WM_HSCROLL:
  70.         case WM_VSCROLL:
  71.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  72.  
  73.             iPos=GetScrollPos(hWnd, idScroll);
  74.             iTmp=iPos;
  75.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  76.  
  77.             switch (wParam)
  78.                 {
  79.                 case SB_LINEUP:     iPos -= 20;  break;
  80.                 case SB_PAGEUP:     iPos -=100;  break;
  81.                 case SB_LINEDOWN:   iPos += 20;  break;
  82.                 case SB_PAGEDOWN:   iPos +=100;  break;
  83.  
  84.                 case SB_THUMBPOSITION:
  85.                     iPos=ScrollThumbPosition(wParam, lParam);
  86.                     break;
  87.  
  88.                 //We don't want scrolling on this message.
  89.                 case SB_THUMBTRACK:
  90.                     return 0L;
  91.                 }
  92.  
  93.             iPos=max(iMin, min(iPos, iMax));
  94.  
  95.             if (iPos!=iTmp)
  96.                 {
  97.                 //Set the new position and scroll the window as necessary.
  98.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  99.  
  100.                 if (SB_HORZ==idScroll)
  101.                     {
  102.                     ppg->m_xPos=iPos;
  103.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  104.                     }
  105.                 else
  106.                     {
  107.                     ppg->m_yPos=iPos;
  108.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  109.                     }
  110.                 }
  111.  
  112.             break;
  113.  
  114.  
  115.         default:
  116.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  117.         }
  118.  
  119.     return 0L;
  120.     }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /*
  129.  * CPages::Draw
  130.  *
  131.  * Purpose:
  132.  *  Paints the current page in the pages window.
  133.  *
  134.  * Parameters:
  135.  *  hDC             HDC to draw on, could be a metafile or printer DC or
  136.  *                  any other type of DC.
  137.  *  fNoColor        BOOL indicating if we should use screen colors or
  138.  *                  printer colos (B&W).  Objects are printed as-is, however.
  139.  *                  This is TRUE for printer DCs or print preview.
  140.  *  fPrinter        BOOL indicating if this is a printer DC in which case
  141.  *                  we eliminate some of the fancy drawing, like shadows on
  142.  *                  the page and so forth.
  143.  *
  144.  * Return Value:
  145.  *  None
  146.  */
  147.  
  148. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  149.     {
  150.     RECT            rc, rcT;
  151.     UINT            uMM;
  152.     HPEN            hPen;
  153.     HBRUSH          hBrush;
  154.     HGDIOBJ         hObj1, hObj2;
  155.     COLORREF        cr;
  156.     char            szTemp[20];
  157.     UINT            cch;
  158.     DWORD           dwExt;
  159.  
  160.     //Make sure the DC is in LOMETRIC
  161.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  162.  
  163.     if (!fPrinter)
  164.         {
  165.         /*
  166.          * We maintain a 6mm border around the page on the screen besides
  167.          * 12.7mm margins.  We also have to account for the scroll position
  168.          * with m_*Pos which are in pixels so we have to convert them.
  169.          */
  170.  
  171.         SetRect(&rcT, m_xPos, m_yPos, 0, 0);
  172.         RectConvertMappings(&rcT, hDC, FALSE);
  173.  
  174.         rc.left  = LOMETRIC_BORDER-rcT.left;
  175.         rc.top   =-LOMETRIC_BORDER-rcT.top;
  176.         }
  177.     else
  178.         {
  179.         /*
  180.          * We define the corner of the printed paper at a negative
  181.          * offset so rc.right and rc.bottom come out right below.
  182.          */
  183.         SetRect(&rc, -(int)m_xMarginLeft, m_yMarginTop, 0, 0);
  184.         }
  185.  
  186.     rc.right =rc.left+(UINT)m_cx+(UINT)(m_xMarginLeft+m_xMarginRight);
  187.     rc.bottom=rc.top -(UINT)m_cy-(UINT)(m_yMarginTop+m_yMarginBottom);
  188.  
  189.     //Draw a rectangle filled with the window color to show the page.
  190.     if (!fPrinter)
  191.         {
  192.         if (fNoColor)
  193.             {
  194.             //Black frame, white box for printed colors.
  195.             hPen  =CreatePen(PS_SOLID, 0, RGB(0,0,0));
  196.             hBrush=CreateSolidBrush(RGB(255, 255, 255));
  197.             }
  198.         else
  199.             {
  200.             //Normal colors on display
  201.             hPen  =CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
  202.             hBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  203.             }
  204.  
  205.         hObj1=SelectObject(hDC, hPen);
  206.         hObj2=SelectObject(hDC, hBrush);
  207.  
  208.         //Paper boundary
  209.         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom+1);
  210.  
  211.         /*
  212.          * Draw a shadow on the *visual* bottom and right edges .5mm wide.
  213.          * If the button shadow color and workspace colors match, then
  214.          * use black.  We always use black when printing as well.
  215.          */
  216.         if (fNoColor)
  217.             cr=RGB(0,0,0);
  218.         else
  219.             {
  220.             cr=GetSysColor(COLOR_BTNSHADOW);
  221.  
  222.             if (GetSysColor(COLOR_APPWORKSPACE)==cr)
  223.                 cr=RGB(0,0,0);
  224.             }
  225.  
  226.         cr=SetBkColor(hDC, cr);
  227.         SetRect(&rcT, rc.left+5, rc.bottom, rc.right+5, rc.bottom-5);
  228.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  229.  
  230.         SetRect(&rcT, rc.right, rc.top-5, rc.right+5, rc.bottom-5);
  231.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  232.         SetBkColor(hDC, cr);
  233.  
  234.         SelectObject(hDC, hObj1);
  235.         SelectObject(hDC, hObj2);
  236.         DeleteObject(hBrush);
  237.         DeleteObject(hPen);
  238.         }
  239.  
  240.     //Write the page number in the lower left corner
  241.     if (!fNoColor)
  242.         {
  243.         SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  244.         SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  245.         }
  246.  
  247.     //Write the page number in our page font.
  248.     cch=wsprintf(szTemp, "Page %d", m_iPageCur+1);
  249.  
  250.     hObj1=SelectObject(hDC, m_hFont);
  251.     dwExt=GetTextExtent(hDC, szTemp, cch);
  252.  
  253.     TextOut(hDC, rc.left+m_xMarginLeft
  254.         , rc.bottom+m_yMarginBottom+HIWORD(dwExt), szTemp, cch);
  255.  
  256.     SelectObject(hDC, hObj1);
  257.  
  258.     //Rectangle to show border.
  259.     MoveTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  260.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.bottom+m_yMarginBottom);
  261.     LineTo(hDC, rc.right-m_xMarginRight, rc.bottom+m_yMarginBottom);
  262.     LineTo(hDC, rc.right-m_xMarginRight, rc.top-m_yMarginTop);
  263.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  264.  
  265.    #ifdef DEBUG
  266.     //X to show diagonals.
  267.     MoveTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  268.     LineTo(hDC, rc.right-m_xMarginRight, rc.bottom+m_yMarginBottom);
  269.     MoveTo(hDC, rc.left+m_xMarginLeft,   rc.bottom+m_yMarginBottom);
  270.     LineTo(hDC, rc.right-m_xMarginRight, rc.top-m_yMarginTop);
  271.    #endif
  272.  
  273.     SetMapMode(hDC, uMM);
  274.     return;
  275.     }
  276.  
  277.  
  278.  
  279.  
  280.  
  281. /*
  282.  * CPages::UpdateScrollRanges
  283.  *
  284.  * Purpose:
  285.  *  Reset scrollbar ranges (horizontal and